package com.test.gwt.mvp.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.web.bindery.event.shared.EventBus;
import com.test.gwt.mvp.client.activity.MailDetailActivityMapper;
import com.test.gwt.mvp.client.activity.MailListActivityMapper;
import com.test.gwt.mvp.client.factory.ClientFactory;
import com.test.gwt.mvp.client.factory.ClientFactoryImpl;
import com.test.gwt.mvp.client.places.AppPlaceHistoryMapper;
import com.test.gwt.mvp.client.places.MailListPlace;
import com.test.gwt.mvp.client.ui.MainScreen;
import com.test.gwt.mvp.client.ui.MainScreenImpl;

public class AppMail implements EntryPoint {

	@Override
	public void onModuleLoad() {
		ClientFactory clientFactory = GWT.create(ClientFactoryImpl.class);
		EventBus eventBus = clientFactory.getEventBus();
		// Initialisation de l’écran principal
		MainScreen mainScreen = new MainScreenImpl();
		
		// Initialisation de la zone contenant la liste des courriels
		MailListActivityMapper maillistMapper = new MailListActivityMapper(clientFactory);
		ActivityManager listActivityManager = new ActivityManager(maillistMapper, eventBus);
		listActivityManager.setDisplay(mainScreen.getMailListContainer());
		
		// Initialisation de la zone contenant le détail des courriels
		MailDetailActivityMapper maildetailmapper = new	MailDetailActivityMapper(clientFactory);
		ActivityManager detailActivityManager = new	ActivityManager(maildetailmapper, eventBus);
		detailActivityManager.setDisplay(mainScreen.getMailDetailContainer());
		
		// Toute cette mécanique s’appuie sur l’historique GWT, il faut donc
		// initialiser ce dernier avec le dictionnaire AppPlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
		PlaceController placeController = clientFactory.getPlaceController();
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, new MailListPlace());
		// On ajoute l’écran principal dans le RootPanel
		RootLayoutPanel.get().add(mainScreen.getMainLayoutPanel());
	}

}
